/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "hdf_audio_server_common.h"

static struct AudioInfoInAdapter g_renderAndCaptureManage[MAX_AUDIO_ADAPTER_NUM_SERVER] = {
    {
        .adapterName = "internal",
        .adapter = NULL,
        .adapterUserNum = 0,
        .renderStatus = 0,
        .renderPriority = -1,
        .render = NULL,
        .renderBusy = false,
        .renderDestory = false,
        .renderPid = 0,
        .captureStatus = 0,
        .capturePriority = -1,
        .capture = NULL,
        .captureBusy = false,
        .captureDestory = false,
        .capturePid = 0
    },
    {
        .adapterName = "usb",
        .adapter = NULL,
        .adapterUserNum = 0,
        .renderStatus = 0,
        .renderPriority = -1,
        .render = NULL,
        .renderBusy = false,
        .renderDestory = false,
        .renderPid = 0,
        .captureStatus = 0,
        .capturePriority = -1,
        .capture = NULL,
        .captureBusy = false,
        .captureDestory = false,
        .capturePid = 0
    },
    {
        .adapterName = "hdmi",
        .adapter = NULL,
        .adapterUserNum = 0,
        .renderStatus = 0,
        .renderPriority = -1,
        .render = NULL,
        .renderBusy = false,
        .renderDestory = false,
        .renderPid = 0,
        .captureStatus = 0,
        .capturePriority = -1,
        .capture = NULL,
        .captureBusy = false,
        .captureDestory = false,
        .capturePid = 0
    }
};

int32_t HdiServiceRenderCaptureReadData(struct HdfSBuf *data, const char **adapterName, uint32_t *pid)
{
    if (adapterName == NULL || data == NULL || pid == NULL) {
        return HDF_FAILURE;
    }
    if ((*adapterName = HdfSbufReadString(data)) == NULL) {
        HDF_LOGE("%{public}s", "adapterName Is NULL");
        return HDF_FAILURE;
    }
    if (!HdfSbufReadUint32(data, pid)) {
        HDF_LOGE("%{public}s", " read buf fail");
        return HDF_FAILURE;
    }
    return HDF_SUCCESS;
}

int32_t AudioAdapterListGetAdapterCapture(const char *adapterName,
    struct AudioAdapter **adapter, struct AudioCapture **capture)
{
    LOG_FUN_INFO();
    if (adapterName == NULL || adapter == NULL || capture == NULL) {
        HDF_LOGE("%{public}s", "The pointer is null");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            *adapter = g_renderAndCaptureManage[i].adapter;
            *capture = g_renderAndCaptureManage[i].capture;
            return HDF_SUCCESS;
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

int32_t AudioDestroyCaptureInfoInAdapter(const char *adapterName)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        return HDF_FAILURE;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            g_renderAndCaptureManage[i].captureStatus = 0;
            g_renderAndCaptureManage[i].capturePriority = -1;
            g_renderAndCaptureManage[i].capture = NULL;
            g_renderAndCaptureManage[i].capturePid = 0;
            return HDF_SUCCESS;
        }
    }
    HDF_LOGE("%{public}s", "AudioDestroycaptureInfoInAdapter: Can not find Adapter!");
    return HDF_FAILURE;
}

int32_t AudioDestroyFormerCapture(struct AudioInfoInAdapter *captureManage)
{
    LOG_FUN_INFO();
    if (captureManage == NULL || captureManage->adapter == NULL || captureManage->capture == NULL) {
        HDF_LOGE("%{public}s", "DestroyFormercapture: input para is NULL.");
        return HDF_FAILURE;
    }
    int count = 0;
    captureManage->captureDestory = true;
    while (captureManage->captureBusy) {
        if (count > 1000) { // Less than 1000
            HDF_LOGE("%{public}s, count = %{public}d", "AudioDestroyFormerRender", count);
            captureManage->captureDestory = false;
            return HDF_ERR_DEVICE_BUSY;
        }
        usleep(500); // sleep 500us
        count++;
    }
    captureManage->capturePid = 0;
    if (captureManage->adapter->DestroyCapture(captureManage->adapter, captureManage->capture)) {
        captureManage->captureDestory = false;
        return HDF_FAILURE;
    }
    captureManage->capture = NULL;
    captureManage->captureStatus = 0;
    captureManage->captureBusy = false;
    captureManage->captureDestory = false;
    captureManage->renderPriority = -1;
    return HDF_SUCCESS;
}

int32_t AudioJudgeCapturePriority(const int32_t priority, int which)
{
    if (which < 0 || which >= MAX_AUDIO_ADAPTER_NUM_SERVER) {
        HDF_LOGE("%{public}s", "AudioJudgeCapturePriority invalid value!");
        return HDF_FAILURE;
    }
    if (!(g_renderAndCaptureManage[which].captureStatus)) {
        return HDF_SUCCESS;
        } else {
        if (g_renderAndCaptureManage[which].capturePriority <= priority) {
            return AudioDestroyFormerCapture(&g_renderAndCaptureManage[which]);
            } else {
            return HDF_ERR_DEVICE_BUSY;
            }
        }
    return HDF_FAILURE;
}

int32_t AudioCreatCaptureCheck(const char *adapterName, const int32_t priority)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        return HDF_FAILURE;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            return AudioJudgeCapturePriority(priority, i);
        }
    }
    HDF_LOGE("%{public}s", "AudioCreatCaptureCheck:Can not find Adapter!");
    return HDF_FAILURE;
}

int32_t AudioAddCaptureInfoInAdapter(const char *adapterName,
    struct AudioCapture *capture,
    struct AudioAdapter *adapter,
    const int32_t priority,
    uint32_t capturePid)
{
    if (adapterName == NULL || adapter == NULL || capture == NULL) {
        HDF_LOGE("%{public}s", "AudioAddcaptureInfoInAdapter: input para is NULL.");
        return HDF_FAILURE;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            g_renderAndCaptureManage[i].captureStatus = 1;
            g_renderAndCaptureManage[i].capturePriority = priority;
            g_renderAndCaptureManage[i].capture = capture;
            g_renderAndCaptureManage[i].capturePid = capturePid;
            HDF_LOGE("%{public}s, (uint64_t)g_renderAndCaptureManage[i].capture = %{public}p",
                "AudioAddcaptureInfoInAdapter: ", g_renderAndCaptureManage[i].capture);
            return HDF_SUCCESS;
        }
    }
    HDF_LOGE("%{public}s", "AudioAddcaptureInfoInAdapter: Can not find Adapter!");
    return HDF_FAILURE;
}

int32_t WriteAudioSampleAttributes(struct HdfSBuf *reply, const struct AudioSampleAttributes *attrs)
{
    if (reply == NULL || attrs == NULL) {
        return HDF_FAILURE;
    }
    uint32_t tempAttrParam = (uint32_t)attrs->type;
    if (!HdfSbufWriteUint32(reply, tempAttrParam)) {
        return HDF_FAILURE;
    }
    tempAttrParam = (uint32_t)attrs->interleaved;
    if (!HdfSbufWriteUint32(reply, tempAttrParam)) {
        return HDF_FAILURE;
    }
    tempAttrParam = (uint32_t)attrs->format;
    if (!HdfSbufWriteUint32(reply, tempAttrParam)) {
        return HDF_FAILURE;
    }
    if (!HdfSbufWriteUint32(reply, attrs->sampleRate)) {
        return HDF_FAILURE;
    }
    if (!HdfSbufWriteUint32(reply, attrs->channelCount)) {
        return HDF_FAILURE;
    }
    if (!HdfSbufWriteUint32(reply, attrs->period)) {
        return HDF_FAILURE;
    }
    if (!HdfSbufWriteUint32(reply, attrs->frameSize)) {
        return HDF_FAILURE;
    }
    tempAttrParam = (uint32_t)(attrs->isBigEndian);
    if (!HdfSbufWriteUint32(reply, tempAttrParam)) {
        return HDF_FAILURE;
    }
    tempAttrParam = (uint32_t)(attrs->isSignedData);
    if (!HdfSbufWriteUint32(reply, tempAttrParam)) {
        return HDF_FAILURE;
    }
    if (!HdfSbufWriteUint32(reply, attrs->startThreshold)) {
        return HDF_FAILURE;
    }
    if (!HdfSbufWriteUint32(reply, attrs->stopThreshold)) {
        return HDF_FAILURE;
    }
    if (!HdfSbufWriteUint32(reply, attrs->silenceThreshold)) {
        return HDF_FAILURE;
    }
    return HDF_SUCCESS;
}

int32_t ReadAudioSapmleAttrbutes(struct HdfSBuf *data, struct AudioSampleAttributes *attrs)
{
    if (data == NULL || attrs == NULL) {
        return HDF_FAILURE;
    }
    uint32_t tempAttrParam;
    if (!HdfSbufReadUint32(data, &tempAttrParam)) {
        return HDF_FAILURE;
    }
    attrs->type = (enum AudioCategory)tempAttrParam;
    if (!HdfSbufReadUint32(data, &tempAttrParam)) {
        return HDF_FAILURE;
    }
    attrs->interleaved = (bool)tempAttrParam;
    if (!HdfSbufReadUint32(data, &tempAttrParam)) {
        return HDF_FAILURE;
    }
    attrs->format = (enum AudioFormat)tempAttrParam;
    if (!HdfSbufReadUint32(data, &(attrs->sampleRate))) {
        return HDF_FAILURE;
    }
    if (!HdfSbufReadUint32(data, &(attrs->channelCount))) {
        return HDF_FAILURE;
    }
    if (!HdfSbufReadUint32(data, &(attrs->period))) {
        return HDF_FAILURE;
    }
    if (!HdfSbufReadUint32(data, &(attrs->frameSize))) {
        return HDF_FAILURE;
    }
    if (!HdfSbufReadUint32(data, &tempAttrParam)) {
        return HDF_FAILURE;
    }
    attrs->isBigEndian = (bool)tempAttrParam;
    if (!HdfSbufReadUint32(data, &tempAttrParam)) {
        return HDF_FAILURE;
    }
    attrs->isSignedData = (bool)tempAttrParam;
    if (!HdfSbufReadUint32(data, &(attrs->startThreshold))) {
        return HDF_FAILURE;
    }
    if (!HdfSbufReadUint32(data, &(attrs->stopThreshold))) {
        return HDF_FAILURE;
    }
    if (!HdfSbufReadUint32(data, &(attrs->silenceThreshold))) {
        return HDF_FAILURE;
    }
    return HDF_SUCCESS;
}

int32_t AudioAdapterListGetAdapter(const char *adapterName, struct AudioAdapter **adapter)
{
    LOG_FUN_INFO();
    if (adapterName == NULL || adapter == NULL) {
        HDF_LOGE("%{public}s", "The pointer is null");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            *adapter = g_renderAndCaptureManage[i].adapter;
            return HDF_SUCCESS;
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

int32_t AudioDestroyFormerRender(struct AudioInfoInAdapter *renderManage)
{
    LOG_FUN_INFO();
    if (renderManage == NULL || renderManage->adapter == NULL || renderManage->render == NULL) {
        HDF_LOGE("%{public}s", "DestroyFormerRender: input para is NULL.");
        return HDF_FAILURE;
    }
    int count = 0;
    renderManage->renderDestory = true;
    while (renderManage->renderBusy) {
        if (count > 1000) { // Less than 1000
            HDF_LOGE("%{public}s, count = %{public}d", "AudioDestroyFormerRender", count);
            renderManage->renderDestory = false;
            return HDF_FAILURE;
        }
        usleep(500); // sleep 500us
        count++;
    }
    renderManage->renderPid = 0;
    if (renderManage->adapter->DestroyRender(renderManage->adapter, renderManage->render)) {
        renderManage->renderDestory = false;
        return HDF_FAILURE;
    }
    renderManage->render = NULL;
    renderManage->renderStatus = 0;
    renderManage->renderBusy = false;
    renderManage->renderDestory = false;
    renderManage->renderPriority = -1;
    return HDF_SUCCESS;
}

int32_t AudioJudgeRenderPriority(const int32_t priority, int which)
{
    if (which < 0 || which >= MAX_AUDIO_ADAPTER_NUM_SERVER) {
        HDF_LOGE("%{public}s", "AudioJudgeRenderPriority invalid value!");
        return HDF_FAILURE;
    }
    if (g_renderAndCaptureManage[which].renderPriority <= priority) {
        if (AudioDestroyFormerRender(&g_renderAndCaptureManage[which])) {
            HDF_LOGE("%{public}s", "AudioDestroyFormerRender: Fail.");
            return HDF_FAILURE;
        }
        return HDF_SUCCESS;
    } else {
        return HDF_ERR_DEVICE_BUSY;
    }
    return HDF_FAILURE;
}

int32_t AudioCreatRenderCheck(const char *adapterName, const int32_t priority)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        return HDF_FAILURE;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            if (!(g_renderAndCaptureManage[i].renderStatus)) {
                return HDF_SUCCESS;
            } else {
                return AudioJudgeRenderPriority(priority, i);
            }
        }
    }
    HDF_LOGE("%{public}s", "AudioCreatRenderCheck:Can not find Adapter!");
    return HDF_FAILURE;
}

int32_t AudioAddRenderInfoInAdapter(const char *adapterName,
    struct AudioRender *render,
    struct AudioAdapter *adapter,
    const int32_t priority,
    uint32_t renderPid)
{
    if (adapterName == NULL || adapter == NULL || render == NULL) {
        HDF_LOGE("%{public}s", "AudioAddRenderInfoInAdapter: input para is NULL.");
        return HDF_FAILURE;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            g_renderAndCaptureManage[i].renderStatus = 1;
            g_renderAndCaptureManage[i].renderPriority = priority;
            g_renderAndCaptureManage[i].render = render;
            g_renderAndCaptureManage[i].renderPid = renderPid;
            return HDF_SUCCESS;
        }
    }
    HDF_LOGE("%{public}s", "AudioAddRenderInfoInAdapter: Can not find Adapter!");
    return HDF_FAILURE;
}

void AudioSetRenderStatus(const char *adapterName, bool renderStatus)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        return;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            g_renderAndCaptureManage[i].renderBusy = renderStatus;
            return;
        }
    }
    HDF_LOGE("%{public}s", "AudioDestroyRenderInfoInAdapter: Can not find Adapter!");
    return;
}

int32_t AudioGetRenderStatus(const char *adapterName)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        return HDF_FAILURE;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            if (!g_renderAndCaptureManage[i].renderDestory) {
                return HDF_SUCCESS;
            } else {
                g_renderAndCaptureManage[i].renderBusy = false;
                return HDF_FAILURE;
            }
        }
    }
    HDF_LOGE("%{public}s", "AudioDestroyRenderInfoInAdapter: Can not find Adapter!");
    return HDF_FAILURE;
}

int32_t AudioDestroyRenderInfoInAdapter(const char *adapterName)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        return HDF_FAILURE;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            g_renderAndCaptureManage[i].renderStatus = 0;
            g_renderAndCaptureManage[i].renderPriority = -1;
            g_renderAndCaptureManage[i].render = NULL;
            g_renderAndCaptureManage[i].renderPid = 0;
            return HDF_SUCCESS;
        }
    }
    HDF_LOGE("%{public}s", "AudioDestroyRenderInfoInAdapter: Can not find Adapter!");
    return HDF_FAILURE;
}

int32_t AudioAdapterListGetPid(const char *adapterName, uint32_t *pid)
{
    LOG_FUN_INFO();
    if (adapterName == NULL || pid == NULL) {
        HDF_LOGE("%{public}s", "The pointer is null");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            HDF_LOGE("%{public}s i = %{public}d", "AudioAdapterListGetPid:", i);
            *pid = g_renderAndCaptureManage[i].renderPid;
            HDF_LOGE("%{public}s pid = %{public}u", "AudioAdapterListGetPid:", *pid);
            return HDF_SUCCESS;
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

int32_t AudioAdapterListGetAdapterRender(const char *adapterName,
    struct AudioAdapter **adapter, struct AudioRender **render)
{
    LOG_FUN_INFO();
    if (adapterName == NULL || adapter == NULL || render == NULL) {
        HDF_LOGE("%{public}s", "The pointer is null");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            *adapter = g_renderAndCaptureManage[i].adapter;
            *render = g_renderAndCaptureManage[i].render;
            return HDF_SUCCESS;
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

int32_t AudioAdapterListGetRender(const char *adapterName, struct AudioRender **render, uint32_t pid)
{
    HDF_LOGE("%{public}s", "AudioAdapterListGetRender in");
    if (adapterName == NULL || render == NULL) {
        HDF_LOGE("%{public}s", "The pointer is null");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            HDF_LOGE("%{public}s renderPid = %{public}u, pid = %{public}u", "AudioAdapterListGetRender",
                g_renderAndCaptureManage[i].renderPid, pid);
            if (g_renderAndCaptureManage[i].renderPid != pid) {
                HDF_LOGE("%{public}s", "AudioAdapterListGetRender renderPid != pid");
                return HDF_ERR_INVALID_OBJECT;
            }
            *render = g_renderAndCaptureManage[i].render;
            return HDF_SUCCESS;
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

int32_t AudioAdapterListGetCapture(const char *adapterName, struct AudioCapture **capture, uint32_t pid)
{
    HDF_LOGE("%{public}s", "AudioAdapterListGetCapture in");
    if (adapterName == NULL || capture == NULL) {
        HDF_LOGE("%{public}s", "The pointer is null");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        HDF_LOGE("%{public}s capturePid = %{public}u, pid = %{public}u", "AudioAdapterListGetcapture",
            g_renderAndCaptureManage[i].capturePid, pid);
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            if (g_renderAndCaptureManage[i].capturePid != pid) {
                HDF_LOGE("%{public}s", "AudioAdapterListGetcapture capturePid != pid");
                return HDF_ERR_INVALID_OBJECT;
            }
            *capture = g_renderAndCaptureManage[i].capture;
            return HDF_SUCCESS;
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

int32_t AudioAdapterListCheckAndGetRender(struct AudioRender **render, struct HdfSBuf *data)
{
    if (render == NULL || data == NULL) {
        return HDF_FAILURE;
    }
    struct AudioRender *renderTemp = NULL;
    const char *adapterName = NULL;
    uint32_t pid;
    if (HdiServiceRenderCaptureReadData(data, &adapterName, &pid) < 0) {
        HDF_LOGE("%{public}s", "HdiServiceRenderStart: HdiServiceRenderCaptureReadData fail");
        return HDF_FAILURE;
    }
    int ret = AudioAdapterListGetRender(adapterName, &renderTemp, pid);
    if (ret < 0) {
        return ret;
    }
    if (renderTemp == NULL) {
        return HDF_FAILURE;
    }
    *render = renderTemp;
    return HDF_SUCCESS;
}

int32_t AudioAdapterListCheckAndGetCapture(struct AudioCapture **capture, struct HdfSBuf *data)
{
    if (capture == NULL || data == NULL) {
        return HDF_FAILURE;
    }
    struct AudioCapture *captureTemp = NULL;
    const char *adapterName = NULL;
    uint32_t pid;
    if (HdiServiceRenderCaptureReadData(data, &adapterName, &pid) < 0) {
        HDF_LOGE("%{public}s", "HdiServiceCaptureStart: HdiServiceRenderCaptureReadData fail");
        return HDF_FAILURE;
    }
    int ret = AudioAdapterListGetCapture(adapterName, &captureTemp, pid);
    if (ret < 0) {
        return ret;
    }
    if (captureTemp == NULL) {
        return HDF_FAILURE;
    }
    *capture = captureTemp;
    return HDF_SUCCESS;
}

int32_t AudioAdapterCheckListExist(const char *adapterName)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        HDF_LOGE("%{public}s", "adapterName is NULL.");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            if (g_renderAndCaptureManage[i].adapterUserNum == 0) {
                return HDF_FAILURE;
            } else if (g_renderAndCaptureManage[i].adapterUserNum > 0) {
                g_renderAndCaptureManage[i].adapterUserNum++;
                return HDF_SUCCESS;
            }
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

int32_t AudioAdapterListDestory(const char *adapterName, struct AudioAdapter **adapter)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        HDF_LOGE("%{public}s", "adapterName is NULL.");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            if (g_renderAndCaptureManage[i].adapterUserNum == 1) {
                g_renderAndCaptureManage[i].adapterUserNum--;
                *adapter = g_renderAndCaptureManage[i].adapter;
                g_renderAndCaptureManage[i].adapter = NULL;
                return HDF_SUCCESS;
            } else if (g_renderAndCaptureManage[i].adapterUserNum > 1) {
                g_renderAndCaptureManage[i].adapterUserNum--;
                return HDF_FAILURE;
            }
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

int32_t AudioAdapterListAdd(const char *adapterName, struct AudioAdapter *adapter)
{
    LOG_FUN_INFO();
    if (adapterName == NULL || adapter == NULL) {
        HDF_LOGE("%{public}s", "adapterName is NULL.");
        return HDF_ERR_INVALID_PARAM;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            g_renderAndCaptureManage[i].adapter = adapter;
            g_renderAndCaptureManage[i].adapterUserNum = 1;
            return HDF_SUCCESS;
        }
    }
    return HDF_ERR_INVALID_PARAM;
}

void AudioSetCaptureStatus(const char *adapterName, bool captureStatus)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        return;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            g_renderAndCaptureManage[i].captureBusy = captureStatus;
            return;
        }
    }
    HDF_LOGE("%{public}s", "AudioSetCaptureStatus: Can not find Adapter!");
    return;
}

int32_t AudioGetCaptureStatus(const char *adapterName)
{
    LOG_FUN_INFO();
    if (adapterName == NULL) {
        return HDF_FAILURE;
    }
    int i;
    for (i = 0; i < MAX_AUDIO_ADAPTER_NUM_SERVER; i++) {
        if (!strcmp(g_renderAndCaptureManage[i].adapterName, adapterName)) {
            if (!g_renderAndCaptureManage[i].captureDestory) {
                return HDF_SUCCESS;
            } else {
                g_renderAndCaptureManage[i].captureBusy = false;
                return HDF_FAILURE;
            }
        }
    }
    HDF_LOGE("%{public}s", "AudioGetCaptureStatus: Can not find Adapter!");
    return HDF_FAILURE;
}

